﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// add
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


namespace FormBase
{
    [Serializable()]
    public class CSerializationSample
    {
        // Serializable的屬性不可有private, protected
        public int Age { get; set; }
        public bool Male { get; set; }
        public string name { get; set; }

        [XmlIgnore]
        public string IgnoreProperty { get; private set; }

        [XmlAttribute]  // to save this property to xml attribute field!
        public string ValueInAttribute { get; set; }

        [NonSerialized()]
        public string ValueNonSerialized;


        public void SetDefault()
        {
            Age = 100;
            Male = false;
            name = "nancy";
            IgnoreProperty = "Ignore";
            ValueInAttribute = "This is put in XML attribute field";
        }
        public bool SerializerToFile(string sFile)
        {
            //using (FileStream stream1 = new FileStream(sFile, FileMode.OpenOrCreate))
            //{
            //    XmlSerializer serializer1 = new XmlSerializer(typeof(CSerializationSample));
            //    serializer1.Serialize(stream1, this);
            //}
            //return CProject.SerializerToFile(sFile, Encoding.UTF8, this);
            return CCommon.SerializerToFile(sFile, Encoding.UTF8, this);
        }
        public bool DescrializeFromFile(string sFile)
        {
            //using (FileStream stream1 = new FileStream(sFile, FileMode.Open))
            //{
            //    XmlSerializer serializer1 = new XmlSerializer(typeof(CSerializationSample));
            //    return (CSerializationSample) serializer1.Deserialize(stream1);
            //}
            //return CProject.DescrializeFromFile(sFile, Encoding.UTF8, this);
            //if (CProject.DescrializeFromFile(sFile, Encoding.UTF8, this)

            //if (sample1 == null)
            //{
            //    CProject.PopupError(CProject.mFunctionStatus.msError);
                
            //    return false;
            //}
            //return true;
            return CCommon.DescrializeFromFile(sFile, Encoding.UTF8, this);
        }
        public string Dump()
        {
            //using (MemoryStream mStream1 = new MemoryStream())
            //{
            //    XmlDocument xmlDoc1 = new XmlDocument();
            //    XmlSerializer serializer1 = new XmlSerializer(typeof(CSerializationSample));
            //    serializer1.Serialize(mStream1, this);
            //    mStream1.Position = 0;
            //    xmlDoc1.Load(mStream1);
            //    return xmlDoc1.InnerXml;
            //}
            return CCommon.DumpSerializableToXML(this);
        }
    }
}
